package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.net.api.AddFriendApi;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;


public class IMAddFriendMsg extends IMSysPopup {

	String mFriendId = "";

	OnRequestSuccessListiener mOnRequestSuccessListiener;
	private InputMethodManager mInputMethodManager;

	public interface OnRequestSuccessListiener{
		void requestSuccess();
	}
	public IMAddFriendMsg(Context pContext, String friendId) {
		super(pContext);
		mFriendId = friendId;
	}

	public IMAddFriendMsg(Context pContext,String friendId,OnRequestSuccessListiener requestSuccessListiener){
		super(pContext);
		mFriendId = friendId;
		mOnRequestSuccessListiener = requestSuccessListiener;
	}
	@Override
	protected void onDismiss(DialogInterface dialog) {

	}

	@Override
	protected void initializeView(View pView) {
		try {
			m_dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mInputMethodManager = (InputMethodManager)m_editMsg.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		m_tvTitle.setText("好友验证请求");
		m_editMsg.setHint("填写申请理由");
		String defaultReason = "";
		String userName = AccountHelper.getUserPhonePOJO().getUserName();
		if (defaultReason == null){
			defaultReason = "我是"+userName;
			m_editMsg.setText(defaultReason);
			m_editMsg.setSelection(2,defaultReason.length());
		}else {
			m_editMsg.setText(defaultReason);
			m_editMsg.setSelection(defaultReason.length());
		}
		m_editMsg.post(new Runnable() {
			@Override
			public void run() {
				mInputMethodManager.showSoftInput(m_editMsg,0);
			}
		});
		m_btnCancel.setText(R.string.im_popup_resend_cancel);
		m_btnAction.setText(R.string.im_popup_clear_history_confirm);
	}

	@Override
	protected void onActionClick() {
		String reason = m_editMsg.getText().toString().trim();
		if(reason.length() == 0){
			ToastUtil.show("请填写添加好友申请消息");
			return;
		}
		ProgressBarUtil.showProgress(m_context);
		RetrofitFactory.getRetrofit().create(AddFriendApi.class)
				.addFriend(AccountHelper.getUidInt(),Integer.valueOf(mFriendId), reason)
				.compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
				.compose(RxUtil.dataNullResult())
				.compose(((RxAppCompatActivity)m_context).<BaseImResponse<Object>>bindToLifecycle())
				.subscribe(new CommonSubscriber<BaseImResponse<Object>>(m_context) {
					@Override
					public void onNext(BaseImResponse<Object> o) {
						if (mOnRequestSuccessListiener!=null){
							mOnRequestSuccessListiener.requestSuccess();
						}

						String message = "好友申请已发送！";
						ToastUtil.show(message);
					}
				});
		dismissDialog();
		hideSoftInput();
	}

	@Override
	protected void onCancelClick() {
		hideSoftInput();
	}

	public void show() {
		showDialog();
	}

	/**
	 * 隐藏软件盘
	 */
	private void hideSoftInput() {
		mInputMethodManager.hideSoftInputFromWindow(m_editMsg.getWindowToken(), 0);
	}
}
